package com.personal.portal.admin.controller;

import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Education;
import com.personal.portal.admin.service.EducationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 教育背景控制器
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Tag(name = "教育背景", description = "教育背景信息管理接口")
@RestController
@RequestMapping("/admin/education")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class EducationController {

    @Resource
    private EducationService educationService;

    /**
     * 根据ID查询教育背景
     */
    @Operation(summary = "根据ID查询教育背景", description = "根据教育背景ID查询详细信息")
    @GetMapping("/{id}")
    public Result<Education> queryById(
            @Parameter(description = "教育背景ID", required = true) 
            @PathVariable Long id) {
        try {
            Education education = educationService.queryById(id);
            if (education != null) {
                return Result.success(education);
            } else {
                return Result.error("教育背景信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询教育背景信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据条件查询教育背景列表
     */
    @Operation(summary = "根据条件查询教育背景列表", description = "根据学校、专业等条件查询教育背景信息")
    @PostMapping("/list")
    public Result<List<Education>> queryList(
            @Parameter(description = "教育背景查询条件") 
            @RequestBody Education education) {
        try {
            List<Education> educationList = educationService.queryList(education);
            return Result.success(educationList);
        } catch (Exception e) {
            return Result.error("查询教育背景列表失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询教育背景
     */
    @Operation(summary = "分页查询教育背景", description = "支持分页和条件查询的教育背景信息")
    @PostMapping("/page")
    public Result<Result.PageResult<Education>> queryPage(
            @Parameter(description = "页码，从1开始", example = "1") 
            @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") 
            @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "教育背景查询条件") 
            @RequestBody Education education) {
        try {
            Result.PageResult<Education> pageResult = educationService.queryPage(pageNum, pageSize, education);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("分页查询教育背景失败: " + e.getMessage());
        }
    }

    /**
     * 创建教育背景
     */
    @Operation(summary = "创建教育背景", description = "新增一条教育背景信息")
    @PostMapping
    public Result<Long> create(
            @Parameter(description = "教育背景信息", required = true) 
            @RequestBody Education education) {
        try {
            int result = educationService.insert(education);
            if (result > 0) {
                return Result.success("创建教育背景成功", education.getId());
            } else {
                return Result.error("创建教育背景失败");
            }
        } catch (Exception e) {
            return Result.error("创建教育背景失败: " + e.getMessage());
        }
    }

    /**
     * 更新教育背景
     */
    @Operation(summary = "更新教育背景", description = "更新指定ID的教育背景信息")
    @PutMapping("/{id}")
    public Result<Long> update(
            @Parameter(description = "教育背景ID", required = true) 
            @PathVariable Long id,
            @Parameter(description = "教育背景信息", required = true) 
            @RequestBody Education education) {
        try {
            education.setId(id);
            int result = educationService.update(education);
            if (result > 0) {
                return Result.success("更新教育背景成功", id);
            } else {
                return Result.error("更新教育背景失败");
            }
        } catch (Exception e) {
            return Result.error("更新教育背景失败: " + e.getMessage());
        }
    }

    /**
     * 删除教育背景
     */
    @Operation(summary = "删除教育背景", description = "根据ID删除指定的教育背景信息")
    @DeleteMapping("/{id}")
    public Result<Long> delete(
            @Parameter(description = "教育背景ID", required = true) 
            @PathVariable Long id) {
        try {
            int result = educationService.deleteById(id);
            if (result > 0) {
                return Result.success("删除教育背景成功", id);
            } else {
                return Result.error("删除教育背景失败");
            }
        } catch (Exception e) {
            return Result.error("删除教育背景失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除教育背景
     */
    @Operation(summary = "批量删除教育背景", description = "根据ID列表批量删除教育背景信息")
    @DeleteMapping("/batch")
    public Result<List<Long>> deleteBatch(
            @Parameter(description = "教育背景ID列表", required = true) 
            @RequestBody List<Long> ids) {
        try {
            List<Long> successIds = educationService.deleteBatch(ids);
            return Result.success(
                String.format("成功删除 %d 条记录", successIds.size()), successIds);
        } catch (Exception e) {
            return Result.error("批量删除教育背景失败: " + e.getMessage());
        }
    }
}